package ui.activity.profit.presenter;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import Bean.AchievementBean;
import base.BaseBiz;
import ui.activity.profit.biz.AchievementFragmentBiz;
import ui.activity.profit.contract.AchievementFragmentContract;
import ui.model.SortAchievementClass;

/**
 * Created by chenjian on 19/2/26
 */

public class AchievementFragmentPresenter implements AchievementFragmentContract.Presenter {

    AchievementFragmentContract.View view;
    AchievementFragmentBiz achievementFragmentBiz;
    List<AchievementBean.DataBean.ItemsBean> list = new ArrayList<>();

    @Inject
    public AchievementFragmentPresenter(AchievementFragmentContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        achievementFragmentBiz = (AchievementFragmentBiz) biz;
    }


    @Override
    public void getInfo() {
        view.loading(true);
        achievementFragmentBiz.getAchievementNumber(new BaseBiz.Callback<AchievementBean.DataBean>() {
            @Override
            public void onSuccess(AchievementBean.DataBean info) {
                view.upDateNumberUI(info.getAllyNumber(), info.getMerchantNumber());
                view.loading(false);
            }

            @Override
            public void onFailure(AchievementBean.DataBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getInfo1(final boolean isRefresh, String year, String mouth) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        achievementFragmentBiz.getAchievementInfo(year, mouth, new BaseBiz.Callback<AchievementBean.DataBean>() {
            @Override
            public void onSuccess(AchievementBean.DataBean info) {
                list.addAll(info.getItems());
                if (list.size() == 3) {
                    SortAchievementClass sort = new SortAchievementClass();
                    Collections.sort(list,sort);
                    view.upDateUI(list);
                }
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(AchievementBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

    @Override
    public void getInfo2(String year, String mouth) {
        view.loading(true);
        achievementFragmentBiz.getAchievementInfo(year, mouth, new BaseBiz.Callback<AchievementBean.DataBean>() {
            @Override
            public void onSuccess(AchievementBean.DataBean info) {
                list.addAll(info.getItems());
                if (list.size() == 3) {
                    SortAchievementClass sort = new SortAchievementClass();
                    Collections.sort(list,sort);
                    view.upDateUI(list);
                }
                view.loading(false);
            }

            @Override
            public void onFailure(AchievementBean.DataBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getInfo3(String year, String mouth) {
        view.loading(true);
        achievementFragmentBiz.getAchievementInfo(year, mouth, new BaseBiz.Callback<AchievementBean.DataBean>() {
            @Override
            public void onSuccess(AchievementBean.DataBean info) {
                list.addAll(info.getItems());
                view.loading(false);
                if (list.size() == 3) {
                    SortAchievementClass sort = new SortAchievementClass();
                    Collections.sort(list,sort);
                    view.upDateUI(list);
                }
            }

            @Override
            public void onFailure(AchievementBean.DataBean info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void getSearchInfo(boolean isRefresh, String year, String mouth) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        achievementFragmentBiz.getAchievementInfo(year, mouth, new BaseBiz.Callback<AchievementBean.DataBean>() {
            @Override
            public void onSuccess(AchievementBean.DataBean info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(AchievementBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }


}
